<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\OssOrderModel;
use share\exception\ApiException;
use share\service\BaseModelService;

class OssOrderService extends BaseModelService
{


    CONST STATE_OF_NOT_PAY = 1;
    CONST STATE_OF_PART_PAY = 2;
    CONST STATE_OF_PAYED = 3;

    CONST STATE_OF_STATEMENT = 7;
    CONST STATE_OF_CLOSE = 8;

    public static function getModel()
    {
        return new OssOrderModel;
    }

    /**
     * 编辑条目 新增或者修改
     */
    public static function edit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        if ($id == 0) {
            self::addOssOrder($info);
        } else {
            self::updateOssOrder($id, $info);
        }
        //TODO add code here after data change
        return $info;
    }

    /**
     * 新增条目
     */
    public static function addOssOrder($info)
    {
        //TODO add code here  
        self::add($info);
    }

    /**
     * 修改条目
     */
    public static function updateOssOrder($id, $info)
    {
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        //TODO  add code here  

        self::update($id, $info);
    }

    /**
     * 删除条目
     */
    public static function delById($id)
    {
        $origin = self::getBase($id);
        if (empty($origin)) {
            return;
        }
        //TODO  add code here  
        $res = self::del($id);
        return $res;
    }

    public static function detail($id){
        $origin = self::getBase($id);
        if (empty($origin)) {
            return null;
        }
        $detail = OssOrderDetailService::getByFilter(['oss_order_id' => $id]);
        return [
            'order' => $origin,
            'detail' => $detail,
        ];
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['order_no']) && !empty($filter['order_no'])) {
            $res['order_no'] = ['like', '%' . $filter['order_no'] . '%'];
        }
        if (isset($filter['out_trade_no']) && !empty($filter['out_trade_no'])) {
            $res['out_trade_no'] = ['like', '%' . $filter['out_trade_no'] . '%'];
        }
        if (isset($filter['user_id']) && !empty($filter['user_id'])) {
            $res['user_id'] = ['like', '%' . $filter['user_id'] . '%'];
        }
        if (isset($filter['iot_id']) && !empty($filter['iot_id'])) {
            $res['iot_id'] = ['like', '%' . $filter['iot_id'] . '%'];
        }
        if (isset($filter['pk']) && !empty($filter['pk'])) {
            $res['pk'] = ['like', '%' . $filter['pk'] . '%'];
        }
        if (isset($filter['dn']) && !empty($filter['dn'])) {
            $res['dn'] = ['like', '%' . $filter['dn'] . '%'];
        }
        if (isset($filter['sn']) && !empty($filter['sn'])) {
            $res['sn'] = ['like', '%' . $filter['sn'] . '%'];
        }
        if (isset($filter['open_app_id']) && !empty($filter['open_app_id'])) {
            $res['open_app_id'] = $filter['open_app_id'];
        }        
        if (isset($filter['product_id']) && !empty($filter['product_id'])) {
            $res['product_id'] = $filter['product_id'];
        }
        if (isset($filter['aliyun_cloud_storage_id']) && !empty($filter['aliyun_cloud_storage_id'])) {
            $res['aliyun_cloud_storage_id'] = $filter['aliyun_cloud_storage_id'];
        }
        if (isset($filter['state']) && !empty($filter['state'])) {
            $res['state'] = $filter['state'];
        }
        if (isset($filter['money']) && !empty($filter['money'])) {
            $res['money'] = $filter['money'];
        }

        return $res;
    }
}
